package com.advertisementsystem.Servie.Impl;

import com.advertisementsystem.Mapper.AdScheduleMapper;
import com.advertisementsystem.Mapper.AdvertisementMapper;
import com.advertisementsystem.Servie.DashboardService;
import com.advertisementsystem.entity.Advertisement;
import com.advertisementsystem.entity.Schedule;
import com.advertisementsystem.vo.AdEffect;
import com.advertisementsystem.vo.DashboardStatsDTO;
import com.advertisementsystem.vo.DashboardVO;
import com.advertisementsystem.vo.RecentAdDTO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class DashboardServiceImpl implements DashboardService {

    @Autowired
    private AdvertisementMapper advertisementMapper;

    @Autowired
    private AdScheduleMapper adScheduleMapper;

    @Override
    public DashboardVO getDashboardData(Long advertiserId) {
        DashboardVO vo = new DashboardVO();

        // 获取统计信息
        vo.setStats(getStats(advertiserId));

        // 获取最近广告
        vo.setRecentAds(getRecentAds(advertiserId));

        return vo;
    }

    private DashboardStatsDTO getStats(Long advertiserId) {
        DashboardStatsDTO stats = new DashboardStatsDTO();

        // 活跃广告数（状态为投放中）
        stats.setActiveAds(advertisementMapper.countActiveAds(advertiserId));

        // 总展示次数（需要根据排期表计算）
        stats.setTotalImpressions(adScheduleMapper.sumImpressions(advertiserId));

        // 总点击次数
        stats.setTotalClicks(adScheduleMapper.sumClicks(advertiserId));

        return stats;
    }

    private List<RecentAdDTO> getRecentAds(Long advertiserId) {
        List<Advertisement> ads = advertisementMapper.selectRecentAds(advertiserId);
        return ads.stream().map(this::convertToRecentAdDTO).collect(Collectors.toList());
    }

    private RecentAdDTO convertToRecentAdDTO(Advertisement ad) {
        RecentAdDTO dto = new RecentAdDTO();
        dto.setId(ad.getId());
        dto.setTitle(ad.getTitle());
        dto.setStatus(ad.getStatus() == 2 ? "active" : "paused");
        dto.setCreatedTime(ad.getCreatedTime());

        LambdaQueryWrapper<Schedule> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Schedule::getAdvertisementId, ad.getId())
                .orderByDesc(Schedule::getCreatedTime);
        List<Schedule> schedule = adScheduleMapper.selectList(queryWrapper);
        Long impressions = 0L;
        Long clicks = 0L;
        if (!schedule.isEmpty()) {
            for (Schedule s : schedule) {
                impressions += s.getImpressions();
                clicks += s.getClicks();
            }

        }

        dto.setImpressions(impressions);
        dto.setClicks(clicks);
        if (impressions == 0)
        {
            dto.setCtr(0.0);
        }
        else {
        dto.setCtr((double) (clicks*100/impressions));
        }

        return dto;
    }
}
